package o;

import android.text.TextUtils;
import com.huawei.appmarket.framework.startevents.bean.SkinResponseBean.SkinResponseInfo;
import com.huawei.appmarket.framework.startevents.control.SkinResourceCache;
import com.huawei.hms.aaid.constant.AaidIdConstant;
import com.huawei.secure.android.common.util.ZipUtil;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import o.fus.a;

public class dia extends Thread {
    private List<SkinResponseInfo> ˎ;
    private int ॱ;

    public dia(List<SkinResponseInfo> list, int i) {
        this.ˎ = list;
        this.ॱ = i;
    }

    public void run() {
        if (!CollectCheck.isEmpty(this.ˎ)) {
            String ˏ = dic.ˏ(this.ॱ);
            dic.ˋ(this.ˎ.size(), this.ॱ);
            int i = 0;
            for (SkinResponseInfo ˋ : this.ˎ) {
                ˋ(ˋ, ˏ, this.ॱ, i);
                i++;
            }
        } else if (dic.ˊ(this.ॱ) != null) {
            dic.ˎ(this.ॱ);
        } else {
            dic.ˎ(this.ॱ);
            dic.ˋ(dic.ˏ(this.ॱ));
        }
    }

    private void ˋ(SkinResponseInfo skinResponseInfo, String str, int i, int i2) {
        if (skinResponseInfo != null) {
            SkinResourceCache ˊ = dic.ˊ(i, i2);
            if (ˊ == null) {
                ˊ = new SkinResourceCache();
            }
            ˊ.ॱ(skinResponseInfo.ˏ());
            ˊ.ॱ(skinResponseInfo.ˎ());
            ˊ.ˊ(skinResponseInfo.ॱ());
            ˊ.ˋ(skinResponseInfo.ˊ());
            ˊ.ˊ("");
            ˊ.ˏ(0);
            ˊ.ˊ(0);
            ˏ(ˋ(skinResponseInfo.ˊ(), str), ˊ);
            dic.ˎ(ˊ, i, i2);
        }
    }

    private String ˋ(String str, String str2) {
        if (TextUtils.isEmpty(str)) {
            return null;
        }
        return str2 + ˏ(str);
    }

    private String ˏ(String str) {
        if (TextUtils.isEmpty(str)) {
            return null;
        }
        return str.substring(str.lastIndexOf("/") + 1, str.length());
    }

    private void ˊ(String str) {
        if (!TextUtils.isEmpty(str) && dic.ˊ(this.ॱ) == null) {
            String substring;
            int lastIndexOf = str.lastIndexOf(".");
            if (lastIndexOf >= 0) {
                substring = str.substring(0, lastIndexOf);
            } else {
                substring = str;
            }
            dic.ˋ(str);
            dic.ˋ(substring);
        }
    }

    private void ˎ(String str, SkinResourceCache skinResourceCache) {
        if (!TextUtils.isEmpty(str) && skinResourceCache != null) {
            String substring;
            int lastIndexOf = str.lastIndexOf(".");
            if (lastIndexOf >= 0) {
                substring = str.substring(0, lastIndexOf);
            } else {
                substring = str;
            }
            if (TextUtils.isEmpty(skinResourceCache.ॱॱ())) {
                ॱ(str, skinResourceCache);
                return;
            }
            File file = new File(substring);
            skinResourceCache.ˏ(substring);
            if (file.exists() && file.isDirectory()) {
                ॱ(file.listFiles(), skinResourceCache);
            }
        }
    }

    private void ˏ(String str, SkinResourceCache skinResourceCache) {
        Closeable fileOutputStream;
        Throwable th;
        Exception exception;
        String ˏ = skinResourceCache.ˏ();
        String ˋ = skinResourceCache.ˋ();
        if (!TextUtils.isEmpty(str) && !TextUtils.isEmpty(ˏ) && !TextUtils.isEmpty(ˋ)) {
            File file = new File(str);
            if (!file.exists()) {
                ˊ(str);
            } else if (ˏ.equalsIgnoreCase(dnw.ˎ(str, AaidIdConstant.SIGNATURE_SHA256))) {
                Logger.ˎ("GetSkinResourceTask", "SkinResource SHA-256 check success.url: " + ˋ);
                ˎ(str, skinResourceCache);
                return;
            } else {
                Logger.ˋ("GetSkinResourceTask", "SkinResource SHA-256 check failed.");
                ˊ(str);
            }
            dic.ˋ(this.ॱ);
            Closeable closeable = null;
            try {
                fileOutputStream = new FileOutputStream(file);
                try {
                    if (ॱ(ˋ, (OutputStream) fileOutputStream)) {
                        Logger.ˏ("GetSkinResourceTask", "download SkinResource success");
                        if (ˏ.equalsIgnoreCase(dnw.ˎ(str, AaidIdConstant.SIGNATURE_SHA256))) {
                            ॱ(str, skinResourceCache);
                        } else {
                            Logger.ˋ("GetSkinResourceTask", "download SkinResource success,but sha256 check failed");
                            dnw.ˊ(new File(str));
                        }
                    } else {
                        Logger.ˋ("GetSkinResourceTask", "download SkinResource failed.");
                        dnw.ˊ(new File(str));
                    }
                    dnw.ˎ(fileOutputStream);
                } catch (FileNotFoundException e) {
                    try {
                        Logger.e("GetSkinResourceTask", "download SkinResource FileNotFoundException error:");
                        dnw.ˎ(fileOutputStream);
                    } catch (Throwable th2) {
                        Throwable th3 = th2;
                        closeable = fileOutputStream;
                        th = th3;
                        dnw.ˎ(closeable);
                        throw th;
                    }
                } catch (Exception e2) {
                    Exception exception2 = e2;
                    closeable = fileOutputStream;
                    exception = exception2;
                    try {
                        Logger.e("GetSkinResourceTask", "download SkinResource Exception error:" + exception.toString());
                        dnw.ˎ(closeable);
                    } catch (Throwable th4) {
                        th = th4;
                        dnw.ˎ(closeable);
                        throw th;
                    }
                }
            } catch (FileNotFoundException e3) {
                fileOutputStream = null;
                Logger.e("GetSkinResourceTask", "download SkinResource FileNotFoundException error:");
                dnw.ˎ(fileOutputStream);
            } catch (Exception e4) {
                exception = e4;
                Logger.e("GetSkinResourceTask", "download SkinResource Exception error:" + exception.toString());
                dnw.ˎ(closeable);
            }
        }
    }

    private boolean ॱ(String str, OutputStream outputStream) {
        InputStream bufferedInputStream;
        OutputStream bufferedOutputStream;
        Exception e;
        Throwable th;
        Throwable th2;
        boolean z = false;
        fvb o_fvb = null;
        if (!TextUtils.isEmpty(str)) {
            try {
                fuv ˊ = dnb.ˊ();
                a ˎ = new a().ˎ(str);
                ˎ.ˋ("Accept-Encoding", "identity");
                fvb execute = ˊ.ˎ(ˎ.ॱ()).execute();
                try {
                    Logger.ˏ("GetSkinResourceTask", "downloadUrlToStream status code:" + execute.ˊ());
                    if (execute.ˏ()) {
                        fuy ᐝ = execute.ᐝ();
                        long ˏ = ᐝ.ˏ();
                        try {
                            bufferedInputStream = new BufferedInputStream(ᐝ.ˎ(), 8192);
                            try {
                                bufferedOutputStream = new BufferedOutputStream(outputStream, 8192);
                                try {
                                    byte[] bArr = new byte[8192];
                                    int i = 0;
                                    while (true) {
                                        int read = bufferedInputStream.read(bArr);
                                        if (read != -1) {
                                            bufferedOutputStream.write(bArr, 0, read);
                                            i += read;
                                        } else {
                                            try {
                                                break;
                                            } catch (Exception e2) {
                                                e = e2;
                                                o_fvb = execute;
                                                try {
                                                    Logger.ˋ("GetSkinResourceTask", "downloadUrlToStream(String urlString,OutputStream outputStream) " + e.toString());
                                                    ˋ(o_fvb, bufferedInputStream, bufferedOutputStream);
                                                    return z;
                                                } catch (Throwable th3) {
                                                    th = th3;
                                                    ˋ(o_fvb, bufferedInputStream, bufferedOutputStream);
                                                    throw th;
                                                }
                                            } catch (Throwable th4) {
                                                th = th4;
                                                o_fvb = execute;
                                                ˋ(o_fvb, bufferedInputStream, bufferedOutputStream);
                                                throw th;
                                            }
                                        }
                                    }
                                    ˋ(execute, bufferedInputStream, bufferedOutputStream);
                                    if (ˏ == ((long) i)) {
                                        z = true;
                                    }
                                    ˋ(execute, null, null);
                                } catch (Throwable th5) {
                                    th2 = th5;
                                    ˋ(execute, bufferedInputStream, bufferedOutputStream);
                                    throw th2;
                                }
                            } catch (Throwable th6) {
                                th2 = th6;
                                bufferedOutputStream = null;
                                ˋ(execute, bufferedInputStream, bufferedOutputStream);
                                throw th2;
                            }
                        } catch (Throwable th7) {
                            th2 = th7;
                            bufferedOutputStream = null;
                            bufferedInputStream = null;
                            ˋ(execute, bufferedInputStream, bufferedOutputStream);
                            throw th2;
                        }
                    } else {
                        ˋ(execute, null, null);
                    }
                } catch (Exception e3) {
                    e = e3;
                    bufferedOutputStream = null;
                    bufferedInputStream = null;
                    o_fvb = execute;
                } catch (Throwable th8) {
                    th = th8;
                    bufferedOutputStream = null;
                    bufferedInputStream = null;
                    o_fvb = execute;
                    ˋ(o_fvb, bufferedInputStream, bufferedOutputStream);
                    throw th;
                }
            } catch (Exception e4) {
                e = e4;
                bufferedOutputStream = null;
                bufferedInputStream = null;
                Logger.ˋ("GetSkinResourceTask", "downloadUrlToStream(String urlString,OutputStream outputStream) " + e.toString());
                ˋ(o_fvb, bufferedInputStream, bufferedOutputStream);
                return z;
            } catch (Throwable th9) {
                th = th9;
                bufferedOutputStream = null;
                bufferedInputStream = null;
                ˋ(o_fvb, bufferedInputStream, bufferedOutputStream);
                throw th;
            }
        }
        return z;
    }

    private void ˋ(fvb o_fvb, InputStream inputStream, OutputStream outputStream) {
        dnw.ˎ(inputStream);
        dnw.ˎ(outputStream);
        dnw.ˎ(o_fvb);
    }

    private void ॱ(String str, SkinResourceCache skinResourceCache) {
        if (!TextUtils.isEmpty(str)) {
            int lastIndexOf = str.lastIndexOf(".");
            if (lastIndexOf >= 0) {
                ॱ(str, skinResourceCache, str.substring(0, lastIndexOf));
            }
        }
    }

    private void ॱ(File[] fileArr, SkinResourceCache skinResourceCache) {
        if (fileArr != null) {
            for (File file : fileArr) {
                if (file != null) {
                    if (file.isDirectory()) {
                        ॱ(file.listFiles(), skinResourceCache);
                    } else {
                        String canonicalPath;
                        try {
                            canonicalPath = file.getCanonicalPath();
                        } catch (IOException e) {
                            Logger.e("GetSkinResourceTask", "getCanonicalPath error");
                            canonicalPath = null;
                        }
                        if (!TextUtils.isEmpty(canonicalPath)) {
                            if (canonicalPath.endsWith(".apk")) {
                                skinResourceCache.ˊ(canonicalPath);
                            } else {
                                String[] split = canonicalPath.split("/");
                                int length = split.length;
                                if (length >= 3) {
                                    Object obj = split[length - 2];
                                    Object obj2 = split[length - 3];
                                    try {
                                        if (!TextUtils.isEmpty(obj)) {
                                            skinResourceCache.ˊ(Integer.parseInt(obj));
                                        }
                                        if (!TextUtils.isEmpty(obj2)) {
                                            skinResourceCache.ˏ(Integer.parseInt(obj2));
                                        }
                                    } catch (NumberFormatException e2) {
                                        Logger.e("GetSkinResourceTask", "pare scene,type error, " + e2.toString());
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    private void ॱ(String str, SkinResourceCache skinResourceCache, String str2) {
        try {
            if (ZipUtil.unZip(str, str2, true)) {
                File file = new File(str2);
                skinResourceCache.ˏ(str2);
                if (file.exists() && file.isDirectory()) {
                    ॱ(file.listFiles(), skinResourceCache);
                }
            }
        } catch (Exception e) {
            Logger.e("GetSkinResourceTask", "doUnZip error, " + e.toString());
        }
    }
}
